<template>
  <div>
    <img :src="codeImg" alt="666"  v-if="codeImg===''"/>
    <span>{{ codeMessage }}</span>
  </div>
  <div class="card_form">
    <div class="login-form">
      <div class="login">
        <h4>login with</h4>
        <div class="solid"></div>
        <div class="social-login">
          <span @click="loginUser">
            <icon-font type="iconwangyiyunyinle_zizhi" />
          </span>
          <span @click="loginPhone">
            <icon-font type="iconshouji" />
          </span>
          <span @click="loginCode">
            <icon-font type="iconerweima" />
          </span>
        </div>
        <div class="fields" v-if="isAccount">
          <div class="username">
            <input v-model="userFrom.userName" type="username" placeholder="Username" />
          </div>
          <div class="password">
            <form action>
              <input
                v-model="userFrom.userPassword"
                type="password"
                placeholder="password"
                autocomplete
              />
            </form>
          </div>
        </div>
        <div class="fields" v-if="!isAccount">
          <div class="username">
            <input type="tel" placeholder="Phone Number" v-model="tel.number" />
          </div>
          <div class="password">
            <form action>
              <input
                type="password"
                placeholder="Verification code"
                autocomplete
                v-model="tel.captcha"
              />
            </form>
          </div>
        </div>
        <button class="custom-btn btn-6" v-if="!isAccount" @click="getPhoneCode">
          <span>Get Code</span>
        </button>
        <button class="custom-btn btn-5" @click="login">
          <span>Login</span>
        </button>
        <div class="link" v-if="isAccount">
          <a href="#">Forgot password?</a> or
          <a href="#">sign up</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { createFromIconfontCN } from '@ant-design/icons-vue'
import { reactive, ref, watch } from 'vue'
import { useStore } from 'vuex'
import localCache from '@/utils/cache'
import { message } from 'ant-design-vue';


const store = useStore()
const codeImg = ref('')
const codeMessage = ref('')


const IconFont = createFromIconfontCN({
  scriptUrl: '//at.alicdn.com/t/font_1793763_r1ad6i271ij.js'
})
const userFrom = reactive({
  userName: localCache.getCache('userName') ?? '',
  userPassword: localCache.getCache('password') ?? '',
})
const tel = reactive({
  number: '',
  captcha: '',
})

const isAccount = ref(true)
const loginPhone = () => {
  isAccount.value = false
}
const isCode = ref(false)
const loginUser = () => {
  isAccount.value = true
}

const loginCode = async () => {
  let i = 0
  isCode.value = true
  await store.dispatch('login/getCodeAction')
  codeImg.value = store.state.login.codeImg
  if (codeImg.value !== '') {
    const timer = setInterval(() => {
      store.dispatch('login/getCodeStatusAction', codeImg.value)
      codeMessage.value = store.state.login.codeMessage
      // console.log(store.state.login.codeMessage);
    }, 500)
    timer
    // console.log(i)
    clearInterval(timer)
  }
}
const getPhoneCode = () => {
  if (tel.number) {
    store.dispatch('login/getPhoneCodeAction', { ...tel })
  } else {
    message.warning('🚀🚀请输入正确的手机号🚀🚀', 1.5);
  }

}
const login = () => {
  localCache.setCache("userName", userFrom.userName)
  localCache.setCache("password", userFrom.userPassword)
  if (isAccount.value) {
    store.dispatch('login/loginAction', { ...userFrom })
  } else {
    if (!tel.captcha) {
      message.warning('🚀🚀请输入正确的验证码!🚀🚀', 1.5);
    } else {
      store.dispatch('login/phoneLoginAction', { ...tel })
    }
  }
}
</script>
<style lang="less" scoped>
img{
  border-radius: 10%;
  margin-left: 220.5px;
 position: absolute;
}
.card_form {
  float: right;
  // background-color: rgb(230, 226, 208);
  width: 350px;
  height: 455px;
  margin: -5px -5px;
  border-radius: 10px;
  border-radius: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #555555;
}

.login-form {
  width: 335px;
  height: 440px;
  padding: 35px 35px 55px 35px;
  border-radius: 40px;
  box-shadow: 13px 13px 20px #346351, -13px -13px 20px #759181;
  background: #ecf0f3;
}
.login {
  background-image: url("../../../assets/img/乐队.jpg");
  background-position: center left;
  background-size: cover;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin: -20px auto;
  box-shadow:
  /* logo shadow */ 0px 0px 2px #5f5f5f,
    /* offset */ 0px 0px 5px #ecf0f3, /* logo shadow */ 8px 8px 15px #a7aaaf,
    /* logo shadow */-8px -8px 15px #ffffff;
}
.solid {
  border-style: solid;
  margin-top: 15px;
  margin-bottom: 2px;
}
.social-login {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 50px;
  margin: 10 20px;
  padding-bottom: -20px;
  font-size: 35px;
}
.social-login span {
  width: 40px;
  height: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 20px;
  cursor: pointer;
  margin: 10px 18px;
}
.social-login span:hover {
  font-size: 60px;
}
.fields {
  width: 100%;
  padding: 15px 5px 5px 5px;
}
.fields input {
  border: none;
  outline: none;
  background: none;
  font-size: 16px;
  color: #555;
  margin-left: -120px;
  width: 300px;
  height: 40px;
  margin-bottom: 16px;
  border-radius: 12.5px;
  box-shadow: inset 8px 8px 8px #cbced1, inset -8px -8px 8px #ffffff;
}
.link {
  text-align: center;
  width: 300px;
  margin-left: -120px;
  font-size: 16px;
}
.link a {
  text-decoration: none;
  color: #2068ce;
}
button {
  margin: 20px;
}
.custom-btn {
  color: #fff;
  border-radius: 5px;
  padding: 10px 25px;
  font-family: "Lato", sans-serif;
  font-weight: 500;
  background: transparent;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  display: inline-block;
  box-shadow: inset 2px 2px 2px 0px rgba(255, 255, 255, 0.5),
    7px 7px 20px 0px rgba(0, 0, 0, 0.1), 4px 4px 5px 0px rgba(0, 0, 0, 0.1);
  outline: none;
}
.btn-5 {
  width: 335px;
  height: 40px;
  line-height: 42px;
  padding: 0;
  border: none;
  margin-left: -143px;
  background: rgb(233, 171, 58);
  // background: linear-gradient(0deg, rgb(240, 166, 7) 0%, rgb(182, 148, 53) 100%);
}
.btn-5:hover {
  color: #f06d17;
  background: transparent;
  box-shadow: none;
}
.btn-5:before,
.btn-5:after {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  height: 2px;
  width: 0;
  background: #f07909;
  box-shadow: -1px -1px 5px 0px #fff, 7px 7px 20px 0px #0003,
    4px 4px 5px 0px #0002;
  transition: 400ms ease all;
}
.btn-5:after {
  right: inherit;
  top: inherit;
  left: 0;
  bottom: 0;
}
.btn-5:hover:before,
.btn-5:hover:after {
  width: 100%;
  transition: 800ms ease all;
}
.btn-6 {
  width: 335px;
  height: 40px;
  line-height: 42px;
  padding: 0;
  margin-top: 10px;
  margin-bottom: 5px;
  border: none;
  margin-left: -143px;
  background: rgb(126, 85, 221);
  // background: linear-gradient(0deg, rgb(240, 166, 7) 0%, rgb(182, 148, 53) 100%);
}
.btn-6:hover {
  color: #5813c7;
  background: transparent;
  box-shadow: none;
}
.btn-6:before,
.btn-6:after {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  height: 2px;
  width: 0;
  background: #6047bd;
  box-shadow: -1px -1px 5px 0px #fff, 7px 7px 20px 0px #0003,
    4px 4px 5px 0px #0002;
  transition: 400ms ease all;
}
.btn-6:after {
  right: inherit;
  top: inherit;
  left: 0;
  bottom: 0;
}
.btn-6:hover:before,
.btn-6:hover:after {
  width: 100%;
  transition: 800ms ease all;
}
</style>
